package com.muchfish.reactor.api;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class ReactorUsingExample {
    public static void main(String[] args) {
        // 使用 using 方法管理资源
        Flux<String> resultFlux = Flux.using(
                () -> getResource(), // 打开资源
                resource -> getData(resource), // 使用资源获取数据流
                resource -> releaseResource(resource) // 关闭资源
        );

        resultFlux.subscribe(System.out::println);
    }

    private static Mono<String> getResource() {
        System.out.println("Opening resource");
        return Mono.just("Resource");
    }

    private static Flux<String> getData(Mono resource) {
        System.out.println("Getting data from resource: " + resource);
        return Flux.just("Data1", "Data2", "Data3");
    }

    private static Mono<Void> releaseResource(Mono resource) {
        System.out.println("Releasing resource: " + resource);
        return Mono.empty();
    }
}

